package CoteClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Vector;

import javax.swing.JOptionPane;

import CoteClient.Vue;
import CoteClient.VueConnexion;
import CoteClient.VueDiscussionInterprete;
import CoteClient.VueDiscussionLocuteur;
import CoteClient.VueInscription;
import CoteClient.VueMenu;
import CoteServeur.Authentification;
import CoteServeur.Langue.Intitule;
import CoteServeur.Langue.Niveau;

public class Controleur {

	private Vue vue;
	private Socket socket;
	private PrintWriter out = null;
	private BufferedReader in = null;
	private Vector<String> vec; // Vecteur qui contient la liste d'amis
	private String profil;
	private String typeCompte;
	private String statutCompte;

	private String adresseServeur = "127.0.0.1";

	// ------------------------------------------------------------------------//
	// ------------------------------------------------------------------------//
	// ----------------------------------VUES----------------------------------//
	// ------------------------------------------------------------------------//
	// ------------------------------------------------------------------------//

	public Controleur() {
		vue = new VueConnexion(this);
		vec = new Vector<String>();
		// try {
		// socket = new Socket(adresseServeur,2009);
		// } catch (UnknownHostException e) {
		// e.printStackTrace();
		// } catch (IOException e) {
		// e.printStackTrace();
		// }
	}

	private void setVue(Vue v) {
		vue = v;
	}

	private Vue getVue() {
		return vue;
	}

	public void fermerVue() {
		System.exit(0);
	}

	public void inscrireCompte() {
		try {
			this.getVue().setVisible(false);
			this.setVue(new VueInscription(this));
			this.getVue().setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void connecterUtilisateur() {
		try {
			this.getVue().setVisible(false);
			this.setVue(new VueConnexion(this));
			this.getVue().setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void lancerMenu() {
		try {

			this.getVue().setVisible(false);
			this.setVue(new VueMenu(this));
			this.getVue().setVisible(true);
			((VueMenu) this.getVue()).alimente();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void lancerDiscussionLocuteur(String ch) {
		try {

			this.getVue().setVisible(false);
			this.setVue(new VueDiscussionLocuteur(this, ch));
			this.getVue().setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void lancerDiscussionInterprete() {
		try {

			this.getVue().setVisible(false);
			this.setVue(new VueDiscussionInterprete(this));
			this.getVue().setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void ajouterContact() {
		try {
			this.getVue().setVisible(false);
			this.setVue(new VueAjoutContact(this));
			this.getVue().setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void ajoutLangues() {
		try {
			this.getVue().setVisible(false);
			this.setVue(new VueAjouterLangues(this));
			this.getVue().setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void supprLangues() {
		try {
			this.getVue().setVisible(false);
			this.setVue(new VueSupprimerLangues(this));
			this.getVue().setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void modifMdp() {
		try {
			this.getVue().setVisible(false);
			this.setVue(new VueModifierMdp(this));
			this.getVue().setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// ------------------------------------------------------------------------//
	// ------------------------------------------------------------------------//
	// ------------------------------------------------------------------------//

	// fonctions traitant les actions dans les vues

	public void inscription(String nomCompte, String organisme,
			Intitule intitule, Niveau niv, String mdp, String confirmerMdp) {
		if (nomCompte.length() != 0 || organisme.length() != 0
				|| mdp.length() != 0 || confirmerMdp.length() != 0) {
			Thread t;
			try {
				socket = new Socket(adresseServeur, 2015);
			} catch (UnknownHostException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				out = new PrintWriter(socket.getOutputStream());
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				out.println("inscr");
				out.flush();
				out.println(nomCompte + "&°&" + organisme + "&°&" + intitule
						+ "&°&" + niv + "&°&" + mdp);
				out.flush();
				t = new Thread(new Emission(out));
				t.start();
				t.join();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

			try {
				in = new BufferedReader(new InputStreamReader(
						socket.getInputStream()));

				Thread t2 = new Thread(new Reception(in, this));
				t2.start();
			} catch (Exception e) {
				System.out.println("Erreur de reception visiblement");
			}

			// this.getVue().setVisible(false);
			// this.setVue(new VueConnexion(this));
			// this.getVue().setVisible(true);

			// if (rechLocuteur(nomCompte) == null) {
			// if (mdp.equals(confirmerMdp)) {
			// // CompteLocuteur cL = new CompteLocuteur(nomCompte,
			// organisme,confirmerMdp, l);
			// locuteurs.put(nomCompte, cL);
			// JOptionPane.showMessageDialog(null,"Compte crée !");
			// connecterUtilisateur();
			// }
			// else {
			// JOptionPane.showMessageDialog(null,"Les deux mots de passes sont différents, veuillez resaisir");
			// }
			// }
			// else {
			// JOptionPane.showMessageDialog(null,"Nom de compte déjà utilisé, veuillez resaisir");
			// }

			// emission des trucs vers le serveur
		} else {
			JOptionPane.showMessageDialog(null,
					"Veuillez saisir tous les champs");
		}
	}

	public void connexion(String ndc, String mdp) {
		if (ndc.length() != 0 || mdp.length() != 0) {
			// if(this.getLocuteur(ndc) != null &&
			// this.getLocuteur(ndc).getMotDePasse().equals(mdp))
			// {
			try {

				socket = new Socket(adresseServeur, 2015);
				out = new PrintWriter(socket.getOutputStream());
				out.println("connect");
				out.flush();
				out.println(ndc + "&°&" + mdp);
				out.flush();
				profil = ndc;
				Thread t = new Thread(new Emission(out));
				t.start();

			} catch (IOException e) {
				e.printStackTrace();
			}

			try {
				in = new BufferedReader(new InputStreamReader(
						socket.getInputStream()));

				Thread t2 = new Thread(new Reception(in, this));
				t2.start();
			} catch (Exception e) {
				System.out.println("Erreur de reception visiblement");
			}

			// JOptionPane.showMessageDialog(null,"Bienvenue, " + ndc);
			// this.lancerMenu();

			// }
			// else
			// {
			// JOptionPane.showMessageDialog(null,"Identifiants invalides");
			// }
		} else {
			JOptionPane.showMessageDialog(null, "Remplir les champs !!");
		}
	}

	public void addContact(String ch) {

		if (ch.isEmpty()) {
			JOptionPane
					.showMessageDialog(null,
							"Remplir le champ de profil pour pouvoir ajouter un contact");
		} else if (!profil.equals(ch)) {
			try {
				socket = new Socket(adresseServeur, 2015);
				out = new PrintWriter(socket.getOutputStream());
				out.println("addContact");
				out.flush();
				out.println(profil + "&°&" + ch);
				out.flush();

				Thread t = new Thread(new Emission(out));
				t.start();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			JOptionPane.showMessageDialog(null,
					"Vous ne pouvez pas vous ajouter !");
		}
		try {
			in = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));

			Thread t2 = new Thread(new Reception(in, this));
			t2.start();
		} catch (Exception e) {
			System.out.println("Erreur de reception visiblement");
		}
	}

	public void infContact(String ch) {
		if (ch.isEmpty()) {
			JOptionPane.showMessageDialog(null, "Selectionnez un contact");
		} else {
			String[] string = ch.split(" ");
			try {
				socket = new Socket(adresseServeur, 2015);
				out = new PrintWriter(socket.getOutputStream());
				out.println("infContact");
				out.flush();
				//System.out.println(string[1]);

				out.println(string[1]);
				out.flush();

				Thread t = new Thread(new Emission(out));
				t.start();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		try {
			in = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));

			Thread t2 = new Thread(new Reception(in, this));
			t2.start();
		} catch (Exception e) {
			System.out.println("Erreur de reception visiblement");
		}
	}

	public void actuCont() {
		try {
			socket = new Socket(adresseServeur, 2015);
			out = new PrintWriter(socket.getOutputStream());
			out.println("actuCont");
			out.flush();
			//System.out.println(profil);

			out.println(profil);
			out.flush();

			Thread t = new Thread(new Emission(out));
			t.start();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			in = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));
			Thread t2 = new Thread(new Reception(in, this));
			t2.start();
		} catch (Exception e) {
			System.out.println("Erreur de reception visiblement");
		}
	}

	public void infProfil(String ch) {
		if (ch.isEmpty()) {
			JOptionPane.showMessageDialog(null, "Selectionnez un contact");
		} else {
			try {
				socket = new Socket(adresseServeur, 2015);
				out = new PrintWriter(socket.getOutputStream());
				out.println("infContact");
				out.flush();
				//System.out.println(ch);

				out.println(ch);
				out.flush();

				Thread t = new Thread(new Emission(out));
				t.start();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		try {
			in = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));

			Thread t2 = new Thread(new Reception(in, this));
			t2.start();
		} catch (Exception e) {
			System.out.println("Erreur de reception visiblement");
		}
	}

	public void informations(String ch) {
		String[] string = decoupe(ch);
		// try {
		// out = new PrintWriter(socket.getOutputStream());
		// } catch (IOException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// out.println("infContact");
		// out.flush();
		// out.println(profil);
		// out.flush();
		//
		// Thread t = new Thread(new Emission(out));
		// t.start();
		if (string[0].equals(profil)) {
			((VueMenu) this.getVue()).compte(string);
		} else {
			((VueMenu) this.getVue()).cont(string);
		}
	}

	public void deconnexion() {
		try {
			socket = new Socket(adresseServeur, 2015);
			out = new PrintWriter(socket.getOutputStream());
			out.println("deco");
			out.flush();
			out.println(profil);
			out.flush();

			Thread t = new Thread(new Emission(out));
			t.start();
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			in = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));

			Thread t2 = new Thread(new Reception(in, this));
			t2.start();
		} catch (Exception e) {
			System.out.println("Erreur de reception visiblement");
		}
	}

	public void quit(String ch) {
		this.getVue().setVisible(false);
		this.setVue(new VueConnexion(this));
		JOptionPane.showMessageDialog(null, ch);
		this.getVue().setVisible(true);
	}

	// public CompteLocuteur rechLocuteur(String nomCompte) {
	// CompteLocuteur cl = locuteurs.get(nomCompte);
	// return cl;
	// }

	// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Fonctions pratiques
	// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public String[] decoupe(String chaineADecoupe) {
		return chaineADecoupe.split("&°&");
	}

	public Vector<String> getVec() {
		return vec;
	}

	public Socket getSocket() {
		return this.socket;
	}

	public void setVec(Vector<String> vec) {
		this.vec = vec;
	}

	public void addVec(String ch) {
		vec.add(ch);
	}

	public String getProfil() {
		return profil;
	}

	public void LancerCom(String ch) {
		if (ch.isEmpty()) {
			JOptionPane.showMessageDialog(null,
					"Selectioner un contact à appeller");
		} else {
//			//System.out
//					.println("----------------------------------------------------------");
//			//System.out.println(socket.getLocalPort() + " "
//					+ socket.getLocalAddress());
//			System.out
//					.println("----------------------------------------------------------");
			if(ch.isEmpty()){
				JOptionPane.showMessageDialog(null, "Selectioner un contact à appeller");
			}
			else
			{
				try {
					socket = new Socket(adresseServeur,2015);
					in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
					
				} catch (UnknownHostException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				//System.out.println("----------------------------------------------------------");
				//System.out.println(socket.getLocalPort() + " " + socket.getLocalAddress());
				//System.out.println("----------------------------------------------------------");
				//System.out.println(this.getTypeCompte() + "------");
				//System.out.println("contact a qui l'envoyer ce message : " + ch);
				if (this.getTypeCompte().equals("0")) {
					String[] string = ch.split(" ");
					this.lancerDiscussionLocuteur(string[1]);
	
					try {
						in = new BufferedReader(new InputStreamReader(
								socket.getInputStream()));
						Thread t2 = new Thread(new Reception(in, this));
						t2.start();
					} catch (Exception e) {
						System.out.println("Erreur de reception visiblement");
					}
					
	//				try
	//				{	
	//					in = new BufferedReader(new InputStreamReader(socket.getInputStream()));	
	//					Thread t2 = new Thread( new Reception(in, this));
	//					t2.start();
	//				}
	//			    catch (Exception e) {
	//			    	System.out.println("Erreur de reception visiblement");
	//			    }
				} else{
					this.lancerDiscussionInterprete();
				}
			}
		}
	}
	

	public String getTypeCompte() {
		return typeCompte;
	}

	public void setTypeCompte(String typeCompte) {
		this.typeCompte = typeCompte;
	}

	public void envoiMess(String text, String profileEnvoi) {

		try {
	//		System.out.println("----------------------------------------------------------");
			//System.out.println(socket.getLocalPort() + " //// " + socket.getLocalAddress());
			//System.out.println("----------------------------------------------------------");
			out = new PrintWriter(socket.getOutputStream());
			out.println("mess");
			out.flush();
			out.println(profil + "&°&" + profileEnvoi + "&°&" + text);
			out.flush();

//			Thread t = new Thread(new Emission(out));
//			t.start();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// try
		// {
		// in = new BufferedReader(new
		// InputStreamReader(socket.getInputStream()));
		//
		// Thread t2 = new Thread( new Reception(in, this));
		// t2.start();
		// System.out.println("Reception d'un message");
		// }
		// catch (Exception e) {
		// System.out.println("Erreur de reception visiblement");
		// }	
		try
		{	
			//in = new BufferedReader(new InputStreamReader(socket.getInputStream()));	
			
			Thread t2 = new Thread( new Reception(in, this));
			t2.start();
			//System.out.println("Reception d'un message");
		}
	    catch (Exception e) {
	    	System.out.println("Erreur de reception visiblement");
	    }
	}

	public void changerStatut(String nouvStatut) {
		try {
			socket = new Socket(adresseServeur, 2015);
			out = new PrintWriter(socket.getOutputStream());
			out.println("chgmtStatut");
			out.flush();
			out.println(profil + "&°&" + nouvStatut);
			out.flush();

			Thread t = new Thread(new Emission(out));
			t.start();
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			in = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));

			Thread t2 = new Thread(new Reception(in, this));
			t2.start();
		} catch (Exception e) {
			System.out.println("Erreur de reception visiblement");
		}
	}

	public void statutChange(String statut) {
		JOptionPane.showMessageDialog(null, statut);
	}

	public String getStatutCompte() {
		return statutCompte;
	}

	public void setStatutCompte(String statutCompte) {
		this.statutCompte = statutCompte;
	}

	public void modifierMdp(String anc, String n, String r) {
		if (anc.length() == 0 || n.length() == 0 || r.length() == 0) {
			JOptionPane.showMessageDialog(null,
					"Veuillez remplir tous les champs");
		} else {
			if (n.equals(r)) {
				try {
					socket = new Socket(adresseServeur, 2015);
					out = new PrintWriter(socket.getOutputStream());
					out.println("modifMdp");
					out.flush();
					out.println(profil + "&°&" + anc + "&°&" + r);
					out.flush();

					Thread t = new Thread(new Emission(out));
					t.start();
				} catch (IOException e) {
					e.printStackTrace();
				}

				try {
					in = new BufferedReader(new InputStreamReader(
							socket.getInputStream()));

					Thread t2 = new Thread(new Reception(in, this));
					t2.start();
				} catch (Exception e) {
					System.out.println("Erreur de reception visiblement");
				}
			} else {
				JOptionPane.showMessageDialog(null, "Mauvais mot de passe");
			}
		}

	}
	
	public void actualiserListe(){
		((VueMenu) this.getVue()).alimente();
	}

}// Fin Controleur